﻿using System.Collections;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using Tzw.Common.Model;

namespace Tzw.Common
{
    public class PrinterHelper
    {
        int _printerP;//默认打印机指针

        [DllImport("winspool.drv")]
        private static extern bool SetDefaultPrinter(string name); //调用win api将指定名称的打印机设置为默认打印机

        /// <summary>
        /// 设置默认打印机
        /// </summary>
        /// <param name="printerName"></param>
        /// <returns></returns>
        public bool SetDefault(string printerName)
        {
            return SetDefaultPrinter(printerName);
        }

        /// <summary>
        /// 修改默认打印机设置
        /// </summary>
        /// <param name="printers"></param>
        public string ChangePrinter(List<PrinterInfo> printers)
        {
            SetDefaultPrinter(printers[_printerP++ % printers.Count].PrinterName);

            return GetDefaultPrinterName();
        }

        /// <summary>
        /// 获取默认打印机名称
        /// </summary>
        /// <returns></returns>
        public string GetDefaultPrinterName()
        {
            return new PrintDocument().PrinterSettings.PrinterName;
        }

        public IEnumerable GetLocalPrinter()
        {
            return PrinterSettings.InstalledPrinters;
        }
    }

}